/*!
 @header PasswordFormViewController.m
 @author 周益
 @version 1.0.0 2013/01/28 Creation
 Copyright (c) 2013年 mypocketbaby. All rights reserved.
 */

//  -业务逻辑细节说明-
//
//  @abstract       PasswordFormViewController用于注册时的密码设定以及找回密码时的密码设定
//

//  @discussion

//  @notes          点击提交时，如果是注册流程，那么下一页就跳向个人信息页，如果是修改密码流程，直接请求服务器，在请求成功后进入登陆页面


//  -类修改日志-
//  修改人   修改时间   针对版本   修改描述


#import "PasswordFormViewController.h"
#import "Config.h"
#import "Helper.h"
#import "RegisterPersonBaseInfoViewController.h"
#import "Option.h"
#import "LaunchViewController.h"
#import "JMFBase.h"

@interface PasswordFormViewController ()

@end

@implementation PasswordFormViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = _SYS_COLOR_BACKGROUND;
    
    //导航按钮：返回
    UIButton *backButton = [Helper buttonWithStyle:ButtonStyleNavigationReturn];
    [backButton addTarget:self action:@selector(backButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    //密码输入框背景
    UIImageView *textFieldImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"com_bg_010"]];
    textFieldImageView.frame = CGRectMake(10, 20, 300, 45);
    [self.view addSubview:textFieldImageView];
    
    //密码输入框
    _textField = [Helper textField:CGRectMake(15, 21, 290, 40) byPlaceholder:_L(@"holder_15")];   //请输入密码
    _textField.font = _SYS_FONT_009;
    _textField.keyboardType = UIKeyboardTypeDefault;
    _textField.secureTextEntry = YES;
    [_textField becomeFirstResponder];
    [self.view addSubview:_textField];
    
    //字数提示
    UILabel *label = [[UILabel alloc] init];
    CGRect labelRect = CGRectMake(250, 85, 60, 30);
    label.frame = labelRect;
    label.font = _SYS_FONT_007;
    label.textColor = _SYS_COLOR_002;
    label.text = _L(@"holder_14");   //6-16位
    label.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label];
    
    //提交按钮
    UIButton *submitButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 130, 300, 44)];
    [submitButton setBackgroundImage:[UIImage imageNamed:@"com_btn_006_n"] forState:UIControlStateNormal];
    submitButton.titleLabel.font = _SYS_FONT_009;
    [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    //如果是注册流程 那么按钮名称为下一步，否则为提交
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isRegisterOrNot"]) {
        [submitButton setTitle:_L(@"btn_yes_11") forState:UIControlStateNormal];
    }else{
        [submitButton setTitle:_L(@"btn_yes_16") forState:UIControlStateNormal];
    }
    
    [submitButton addTarget:self action:@selector(submitButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:submitButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 事件

/*!
 @abstract 返回按钮事件
 */
- (void)backButtonEvent:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


/*!
 @abstract 提交按钮事件
 */
- (void)submitButtonEvent:(id)sender
{
//    RegisterPersonBaseInfoViewController *setPersonalBaseInfoVC = [[RegisterPersonBaseInfoViewController alloc] init];
//    [self.navigationController pushViewController:setPersonalBaseInfoVC animated:YES];
    //用户输入情形判断
    if (_textField.text && [_textField.text length] >= 6 && [_textField.text length] <= 16 && [self validatePassword:_textField.text]) {
        
        //注册情形
        NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
        if ([userDefaults boolForKey:@"isRegisterOrNot"]) {
            
            //暂存密码
            [userDefaults setValue:_textField.text forKey:@"accountPassword"];
            
            RegisterPersonBaseInfoViewController *setPersonalBaseInfoVC = [[RegisterPersonBaseInfoViewController alloc] init];
            [self.navigationController pushViewController:setPersonalBaseInfoVC animated:YES];
        
        }
        //忘记密码情形
        else{
            
            //向服务器发送验证并且修改密码请求
            JSONRequest *request = [[JSONRequest alloc] initWithURLString:_JSON_URL_U_CHANGE_PASSWORD];
            request.delegate = self;
            request.hudOn = true;
            request.hudTitle = _L(@"title_loading_05");
            
            NSString *mobile = (NSString *)[userDefaults objectForKey:@"accountMobile"];
            [request setValue:mobile forParameter:@"mobile"];
            [request setValue:_textField.text forParameter:@"password"];
            [request setValue:[userDefaults objectForKey:@"code"] forParameter:@"code"];
            [request asynchronous];
        }

    }
    else{
        [Helper showAlertWithTitle:nil andMessage:_L(@"alert_38")];  //请您按要求填写密码
    }
    
}

/*!
 @abstract 验证密码，6-16位，由大写或小写字母或者数字组成
 @param mobile 手机号码
 @result BOOL类型，返回值为true时，表示手机验证通过，否则为不通过。
 */
- (BOOL)validatePassword:(NSString *)mobile
{
    NSString *codeRegex = @"^[A-Za-z0-9]{6,16}$";;
    NSPredicate *authCodeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",codeRegex];
    return [authCodeTest evaluateWithObject:mobile];
}

#pragma mark - JSONRequestDelegate

//
- (void)requestDidLoadJSON:(NSDictionary *)responseData
{
    JMFBase* jmf = [[JMFBase alloc] initWithJSONDictionary:responseData];
    
    if (jmf.status == JMStatusSucceed) {
        
        //进入登录页面
//        LaunchViewController *lauchVC = [[LaunchViewController alloc] init];
//        [self.navigationController presentModalViewController:lauchVC animated:YES];
        [self.navigationController popToRootViewControllerAnimated:true];
    }
    else {
        [Helper easyShowJMFStatus:jmf];
    }
}

//
- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:nil andMessage:_L(@"title_networkunavailabe")];
}

@end
